var AreaUtil  = function ($selector,inputName,level) {
    this._$selector = $selector;
    this._inputName = inputName;
    this._level = level;
    this.options = {
        "url" : {
            "root" : rootPath + "s/sys/area/root",
            "child" : rootPath + 's/sys/area/child'
        }
    }
}
AreaUtil.map = {};
$.extend(AreaUtil.prototype,{
	getAreaCodeMap : function() {
		$.ajax({
            url : rootPath + 's/sys/area/areacodemap',
            type : 'get',
            async :false,
            success : function (res) {
            	AreaUtil.map = res;
            }
        });
		return AreaUtil.map;
	},
    init : function (areaCode) {
        var _this = this;
        var rootArea  = "";
        var $rootSelect = "";
        $.ajax({
            url : _this.options.url.root ,
            type : 'get',
            async :false,
            success : function (res) {
                if(res.opStatus == '0'){
                    rootArea = res.data.areaCode;
                    $rootSelect = $('<select disabled class="form-control" id="area_root_select"><option value="'+res.data.areaCode+'">'+res.data.areaName+'</option></select>');
                    $(_this._$selector).append($rootSelect);
                }
            }
        });

        _this.appendChild($("#area_root_select"),areaCode);

        var lastZeroIndex = 0;
        for(var i = rootArea.length - 1 ; i > 0 ;i--){
            var chart = rootArea.indexOf(i);
            if(chart != '0'){
                lastZeroIndex = i;
                break;
            }
        }


    },
    appendChild : function($selector,selectCode){
        var _this  = this;
        var parent = $($selector).find("option:selected").val();

        $.ajax({
            url : _this.options.url.child ,
            type : 'get',
            async :false,
            data : {parent : parent},
            success : function (res) {
                if(res.opStatus == '0'){
                    var level = res.data[0].level;
                    var $select = $('<select class="form-control" id="area_level'+level+'"></select>');
                    if(level < _this._level){
                        $select.on('change',function () {
                            $("#area_level" + (level +1)).empty();
                            _this.appendChild($("#area_level" + (level +1)));
                        })
                    }else{
                        $select.attr("name",_this._inputName);
                        $select.find("option[value='"+selectCode+"']").attr("checked",true);
                    }
                    $select.append("<option value=''>--请选择--</option>");
                    for(var i =0 ; i < res.data.length ; i++){
                        var area = res.data[i];
                        var str = '<option value="'+area.areaCode+'">'+area.areaName+'</option>';
                        $select.append(str);
                    }

                    $(_this._$selector).append($select);
                }
            }
        })
    }
});